/**
 * 
 */
package parser.nodes;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

import parser.AstNode;
import parser.Memory;
import parser.StringMatch;
import parser.StringMatchSet;

/**
 * @author greg
 *
 */
public class MaxFreqStringNode extends AstNode {
	
	
	@Override
	protected Object executeNode(HashMap<String,Object> mem, Object lval, Object rval) {
		if((lval instanceof String)) {
			lval = mem.get(lval);
		}
		StringMatchSet smset = (StringMatchSet)lval;
		int maxCount = 0;
		HashMap<String,Integer> counts = new HashMap<String,Integer>();
		String maxStr = null;
		for(StringMatch sm : smset.matchSet) {
			String curr = sm.value;
			if(!counts.containsKey(curr))
				counts.put(curr, 1);
			else
				counts.put(curr, counts.get(curr)+1);
		}
		for(Entry<String, Integer> entry:counts.entrySet())
		{
			if(entry.getValue()>maxCount)
			{
				maxStr = entry.getKey();
				maxCount = entry.getValue();
			}
		}
		
		
		return maxStr;
	}

}
